# 创建和使用类
## 创建 dog 类
class Dog:
    """一次模拟小狗的简单尝试"""
    def __init__(self,name,age):   ##构造函数
        """初始化 name 和 age"""
        self.name = name
        self.age = age

    def sit(self):
        """模拟小狗收到命令做下"""
        print(f"{self.name} is now sitting")

    def roll_over(self):
        """模拟小狗收到命令打滚"""
        print(f"{self.name} rolled over!")

## 创建实例
my_dog = Dog("willie",6)
print(f"My dog's name is {my_dog.name}")
print(f"My dog is {my_dog.age} yesars old")

### 访问属性
my_dog = Dog("willie",6)
print(my_dog.name)

### 调用方法
my_dog = Dog("willie",6)
my_dog.sit()
my_dog.roll_over()

###创建多个实例
my_dog = Dog("willie",6)
your_dog = Dog("lucy",3)
print(f"My dog's name is {my_dog.name}")
print(f"My dog is {my_dog.age} yesars old")
my_dog.sit()
print(f"\nyour dog's name is {your_dog.name}")
print(f"your dog is {your_dog.age} yesars old")
your_dog.sit()


# 使用类和实例
class Car:
    """模拟一辆汽车"""
    def __init__(self,make,model,year):
        """初始化"""
        self.make = make
        self.model = model
        self.year = year

    def get_descriptive_name(self):
        """返回格式规范的描述信息"""
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name

my_new_car = Car("audi","a4","2024")
print(my_new_car.get_descriptive_name())

## 给属性指定默认值
class Car:
    """模拟一辆汽车"""
    def __init__(self,make,model,year):
        """初始化"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        """返回格式规范的描述信息"""
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name

    def read_odometer(self):
        """打印里程信息"""
        print(f"This car has {self.odometer_reading} miles on it")

    def update_odometoer(self,mileage):
        """将里程表更新为指定值
        禁止回调"""
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self,miles):
        """让里程表读数增加指定的量"""
        self.odometer_reading += miles

my_new_car = Car("audi","a4","2024")
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()

## 修改属性值
### 直接修改
my_new_car = Car("audi","a4","2024")
print(my_new_car.get_descriptive_name())
my_new_car.odometer_reading = 23
my_new_car.read_odometer()

###通过方法修改
"""添加函数def update_odometoer(self,mileage):"""
my_new_car = Car("audi","a4","2024")
print(my_new_car.get_descriptive_name())
my_new_car.update_odometoer(50)
my_new_car.read_odometer()
my_new_car.update_odometoer(10)
my_new_car.read_odometer()

### 通过方法让属性值递增
"""添加函数def increment_odometer(self,miles):"""
my_used_car = Car("subaru","outback",2019)
print(my_used_car.get_descriptive_name())
my_used_car.update_odometoer(23_500)
my_used_car.read_odometer()
my_used_car.increment_odometer(100)
my_used_car.read_odometer()


#继承
## 子类的__init__()方法 通常要调用父类的 init 方法
class Car:
    """模拟一辆汽车"""
    def __init__(self,make,model,year):
        """初始化"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        """返回格式规范的描述信息"""
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name

    def read_odometer(self):
        """打印里程信息"""
        print(f"This car has {self.odometer_reading} miles on it")

    def update_odometoer(self,mileage):
        """将里程表更新为指定值
        禁止回调"""
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self,miles):
        """让里程表读数增加指定的量"""
        self.odometer_reading += miles

    def fill_gas_tank(self):
        """给子类重写做例子"""
        print("加满油！")

class ElectricCar(Car):
    """电动车的独特之处"""
    def __init__(self,make,model,year):
        """初始化父类的属性"""
        super().__init__(make,model,year)

my_leaf = ElectricCar("nissan","leaf",2024)
print(my_leaf.get_descriptive_name())

## 给子类定义属性和方法
class ElectricCar(Car):
    """电动车的独特之处"""
    def __init__(self,make,model,year):
        """初始化父类的属性"""
        super().__init__(make,model,year)
        self.battery_size = 40

    def describe_battery(self):
        """打印电池容量信息"""
        print(f"This car has a {self.battery_size}-kWh battery")

my_leaf = ElectricCar("nissan","leaf",2024)
print(my_leaf.get_descriptive_name())
my_leaf.describe_battery()

## 重写父类方法
class ElectricCar(Car):
    """电动车的独特之处"""
    def __init__(self,make,model,year):
        """初始化父类的属性"""
        super().__init__(make,model,year)
        self.battery_size = 40

    def describe_battery(self):
        """打印电池容量信息"""
        print(f"This car has a {self.battery_size}-kWh battery")

    def fill_gas_tank(self):
        """电动车没有油箱"""
        print("This car doesn't have a gas tank")

my_leaf = ElectricCar("nissan","leaf",2024)
print(my_leaf.get_descriptive_name())
my_leaf.fill_gas_tank()


## 将实例用作属性
class Battery:
    """一次模拟电动汽车电池的简单尝试"""
    def __init__(self,battery_size = 40):
        """初始化电池属性"""
        self.battery_size = battery_size

    def describe_battery(self):
        """打印一条描述电池容量的消息"""
        print(f"This car has a {self.battery_size}-kWh battery")

    def get_range(self):
        """打印信息指出电池续航里程"""
        if self.battery_size == 40:
            range = 150
        elif self.battery_size == 65:
            range = 225
        print(f"This car can go about {range} miles on a full charge")

class ElectricCar(Car):
    """电动车独特之处"""
    def __init__(self,make,model,year):
        super().__init__(make,model,year)
        self.battery = Battery()

my_leaf = ElectricCar("nissan","leaf",2024)
print(my_leaf.get_descriptive_name())
my_leaf.battery.describe_battery()
my_leaf.battery.get_range()


# 导入类
## 导入单个类
"""通过 import 导入类，详见项目文件夹 09_类_import_example: car.py & my_car.py"""

## 在一个模块存储多个类
"""通过 import 导入类，详见项目文件夹 09_类_import_example: car2.py & my_car2.py"""

## 从一个模块导入多个类
"""通过 import 导入类，详见项目文件夹 09_类_import_example: car2.py & my_cars.py"""

## 导入整个模块
"""通过 import 导入类，详见项目文件夹 09_类_import_example: car2.py & my_cars2.py"""

## 导入模块所有类
"""通过 import 导入类，详见项目文件夹 09_类_import_example: car2.py & my_cars3.py"""
""""不建议使用 同名有可能导致错误"""

## 在一个模块中导入另一个模块
"""通过 import 导入类，详见项目文件夹 09_类_import_example: car.py electric_car.py & my_car3.py"""

## 使用别名
"""通过 import 导入类，详见项目文件夹 09_类_import_example: electric_car.py & my_car4.py"""

# 标准库
"""示例 详见项目文件夹 09_类_import_example: standard_example.py"""